package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	a := People{
		name: "aaa",
		age:  19,
	}
	m.Store("a", &a)
	fmt.Printf("store %p\n", &a)
	go readMap("a")
	go readAndUpdateMap("a")
	time.Sleep(1 * time.Second)
}

var m sync.Map

type People struct {
	name string
	age int
}

func readMap(key interface{})  {
	a, ok := m.Load(key)
	if !ok {
		panic("read failed")
	}
	fmt.Printf("read Map %p\n", a)
	fmt.Println("read Map ", a)
}

func readAndUpdateMap(key interface{}) {
	load, ok := m.Load(key)
	if !ok {
		panic("readAndUpdateMap read failed")
	}
	a := load.(*People)
	a.name = "a"
	m.Store("a", a)
}

type ListNode struct {
	Val int
	Next *ListNode
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	if l1 == nil && l2 == nil {
		return nil
	}
	carry := false
	result := 0
	temp := 0
	if l1 != nil {
		temp += l1.Val
	}
	if l2 != nil {
		temp += l2.Val
	}

	if temp >= 10 {
		carry = true
		result = temp - 10
	} else {
		result = temp
	}

	if carry {
		if l1 != nil {
			if l1.Next == nil {
				l1.Next = new(ListNode)
				l1.Next.Val = 1
			} else {
				l1.Next.Val ++
			}
		} else if l2 != nil {
			// 如果l1 是 nil，那么l2必定不是nil
			if l2.Next == nil {
				l2.Next = new(ListNode)
				l2.Next.Val = 1
			} else {
				l2.Next.Val ++
			}
		}
	}

	a := new(ListNode)
	a.Val = result
	if l1 != nil {
		l1 = l1.Next
	}
	if l2 != nil {
		l2 = l2.Next
	}
	a.Next = addTwoNumbers(l1, l2)
	return a
}